/* ============================================================================
* 功能描述 ：主函数
* 作者       ：ggls
* 创建日期 ：2024.04.10
* ----------------------------------------------------------------------------
* 更新历史 ：
*	日期 　　- 修改者   : 更新内容描述
*
* ============================================================================
*/


#include "main.h"
unsigned char RxTable[10],rxDADA;
bit time_f;
char now_time_f;

char last_time_f;

bit key_flag;

bit RXOK_f;

static date_time_t data1;



static void _data_tx_pack(void)
{
#if 0
	char app_data[10];
	unsigned int ss;
	ss = Read_ADC(14, 0);
	app_data[0] = SHORT_GET_HIGH_BYTE(ss);
	app_data[1] = SHORT_GET_LOW_BYTE(ss);
	app_data[2] = ss;
	app_data[3] = 0x04;
	app_data[4] = 0x05;
	app_data[5] = 0x06;
	app_data[6] = 0x06;
	app_data[7] = 0x06;
	app_data[8] = 0x06;
	Uart_Tx_buf(app_data, 3);

#else
	char app_data[10];
	app_data[0] = 1;
	app_data[1] = 1;
	app_data[2] = 0x99;
	app_data[3] = 0x04;
	app_data[4] = 0x05;
	app_data[5] = 0x06;
	app_data[6] = 0x06;
	app_data[7] = 0x06;
	app_data[8] = 0x06;
	Uart_Tx_buf(app_data, 3);

#endif
}


void main(void)
{
	int i;
	Init_System();
	Time1_Init();

	Uart_Init();
	
	Zigbee_App_Init();	
	LCD_I2C_init();
	ADC_INIT();
	
	Zigbee_wake_up();	

	WPUA5 = 1;
	TRISA5 = 1;

	now_time_f = 0;
	key_flag = 0;
	last_time_f = 0;
	time_f = 0;

	Seg_Init();

	Seg_Set_Data();

	
	DelayXms(15);
	Hly_Temp_Hum_Obtain();
	DelayXms(15);
	Hly_Zigbee_Restart();
	app_init();

	while(1)
	{

	
	
#if 1

		HLY_GetTick();
		if(RA5 == 0)
		{
			DelayXms(10);  // 防抖


			if(RA5 == 0)
			{
				if(key_flag == 0)
				{
					last_time_f = now_time_f;
					time_f = 1;
				}
				if(abs(now_time_f - last_time_f) == 5 && time_f == 1)
				{
					_data_tx_pack();
					time_f = 0;
				}
				key_flag = 1;
			}
			
		}
		else
		{
			DelayXms(10);        
			if(RA5 == 1 && key_flag == 1 && time_f == 1)   //检测弹起
			{
				//_data_tx_pack();             //弹起后处理任务
				Hly_Temp_Hum_Obtain();
				time_f = 0;
				key_flag = 0;
				DelayXms(10);		 
				Write_Lcd_Storage(hum_100 , 4);
				Write_Lcd_Storage(min_1, 5);
				Write_Lcd_Storage(min_2, 5);
				Write_Lcd_Storage(hour_1, 5);
				Write_Lcd_Storage(hour_2, 2);
			}			
		}
#endif
#if 0



	//Hly_Temp_Hum_Obtain();
			DelayXms(200);
			App_Run();
			DelayXms(200);
		//	Zigbee_sleep();
		//	asm("sleep");				//进入休眠模式
			//key_on();

			RB5 = 0;
		RB4 = 0;
		
		RB5 = ~RB5;
		RB4 = ~RB4;
		_nop();
		_nop();
		RB5 = ~RB5;
		RB4 = ~RB4;
		_nop();
		
		_nop();
		RB5 = ~RB5;
		RB4 = ~RB4;
		_nop();
		_nop();
		
		RB5 = ~RB5;
		RB4 = ~RB4;

		i = TMR1L;
		if (i == 0xff)
		{
		
			_data_tx_pack();
			TMR1L = 0;
			TMR1H = 0;
			time_f++;
		}
			
#endif	

		
		
		
		

		
#if 0
		if(RXOK_f==1)
		{
			for(i=0;i<8;i++)
			{
					
				TXREG =RxTable[i];

				while(TRMT==0);
			}

			RXOK_f = 0;
		}
#endif
	}
}
void key_on(void)
{
	if(RA5 == 0)
		{
			DelayXms(10);  // 防抖


			if(RA5 == 0)
			{
				last_time_f = now_time_f;
				if(abs(now_time_f - last_time_f) == 5)
				{
					HLY_GetTick();
				}
				key_flag = 1;
			}
			
		}
		else
		{
			DelayXms(10);        
			if(RA5 == 1 && key_flag == 1)   //检测弹起
			{
				//_data_tx_pack();             //弹起后处理任务
				Hly_Temp_Hum_Obtain();
				time_f = 0;
				
			}			
		}

}


/***********************************************
函数名称：Init_System
函数功能：系统初始化
入口参数：无
出口参数：无
备注：
************************************************/
void Init_System()
{
	asm("nop");
	asm("clrwdt");
	INTCON = 0;					//系统初始化
	OSCCON = 0X70;				//配置振荡为16M,关WDT
	OPTION_REG = 0;

	PEIE = 1;
	RCIE = 1;
	GIE  = 1;

}


/***********************************************
函数名称：Time1_Init
函数功能：time1初始化
入口参数：无
出口参数：无
备注：
************************************************/

void Time1_Init()
{
	
	
	
	T1GINV = 0;
	
	TMR1GE = 0;	
	
	T1CKPS0 = 0;
	T1CKPS1 = 0;
	
	
	T1SYNC = 1;

	
	TMR1CS = 1;
	
	T1OSCEN = 1;
	TMR1ON = 1;
	
	DelayXms(100);
	TMR1H = 0;
	TMR1L = 0;

	TMR1IE = 1;


	ANS8 = 1;
	ANS9 = 1;	
	DelayXms(10);

}


/***********************************************
函数名称：PB_Isr
函数功能：PB电平变化中断服务
入口参数：无
出口参数：无
备注：
************************************************/
void interrupt PB_Isr()
{
	if(RBIF)
	{
		RBIF = 0;			//清中断标志
		RB6 = ~RB6;
	
	}
	if (TMR1IF)
	{
		TMR1L = 0;
		TMR1H = 0;
		TMR1IF = 0;
	//	_data_tx_pack();
	}

#if 1	
	if(RCIF==1)					// 接收中断
	{
		uart_cmd_rx_cplt_callback();
	}

#else
	static unsigned char RxNum=0,TEMP;

	if(RCIF==1)					// 接收中断
	{
		RCIF = 0;
	    
		if(RXOK_f==0)	
		{
			RxTable[RxNum] = RCREG;
			RxNum++;
			if(RxNum > 7)
			{
				RxNum =0;
				RXOK_f =1;
			}
		}
		else
		TEMP =  RCREG;
		
	}
#endif
}

/***********************************************
函数名称：HLY_GetTick
函数功能：获取当前时间
入口参数：无
出口参数：无
备注：
************************************************/

void HLY_GetTick(void)
{
	if(TMR1H == 0x7f)
	{
		TMR1L = 0;
		TMR1H = 0;
		now_time_f++;
		if(now_time_f == 200)
		{
			now_time_f = 0;
		}
	}

}

/***********************************************
函数名称：Hly_stamp_to_time
函数功能：时间戳转化为日期
入口参数： [in] date 日期值
出口参数：=0 成功 非0 失败，以及失败原因
备注：
************************************************/
unsigned int  Hly_stamp_to_time( unsigned int  timep, date_time_t *date)
{
    unsigned int  days = 0;
    unsigned int  rem = 0;
 
    /* 北京时间补偿 */
    timep = timep + 8*60*60;
 
    // 计算天数
    days = (unsigned int )(timep / 86400);
    rem = (unsigned int )(timep % 86400);
 
 
    // 计算年份
    unsigned short year;
    for (year = 1970; ; ++year)
    {
        unsigned short leap = ((year % 4 == 0) && (year % 100 != 0)) || (year % 400 == 0);
        unsigned short ydays = leap ? 366 : 365;
        if (days < ydays)
        {
            break;
        }
        days -= ydays;
    }
    date->year  =  year;
 
 
    // 计算月份
    static const unsigned short days_in_month[] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
    unsigned short month;
    for (month = 0; month < 12; month++)
    {
        unsigned short mdays = days_in_month[month];
        if (month == 1 && ((year % 4 == 0) && (year % 100 != 0)) || (year % 400 == 0))
        {
            mdays = 29;
        }
        if (days < mdays)
        {
            break;
        }
        days -= mdays;
    }
    date->month = month;
    date->month += 1;
 
    // 计算日期
    date->day = days + 1;
 
    // 计算时间
    date->hour = rem / 3600;
    rem %= 3600;
    date->min = rem / 60;
    date->sec = rem % 60;
 
    return 0;
}


void hly_time_start(void)
{
	data1.sec++;
	if(data1.sec == 60)
	{
		data1.sec = 0;
		data1.min++;
	}
	if(data1.min == 60)
	{
		data1.min = 0;
		data1.hour;
	}
	if(data1.hour == 24)
	{
		data1.hour = 0;
	}


}
